/*
 * [Ronny Abraham]
 * [ronnya@gmail.com]
 * CS530:DUI, Assignment [#1]
 */

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ActorsController 
{

	ActorsController(ActorsModel actorsModel, PreviewController previewController, JFrame frame)
	{
		itsActorsModel = actorsModel;
		itsPreviewController = previewController;
	}
	
	public JPanel createView()
	{
		itsActorsView = new ActorsView(itsActorsModel.itsActorTableModel, this);
		return itsActorsView.itsActorsPanel;
	}

	public void rowSelected(int index)
	{
		if(index >-1 && index < itsActorsModel.itsActorTableModel.actorsVector.size())
		{
			Actor actor = itsActorsModel.itsActorTableModel.actorsVector.get(index);
			itsPreviewController.setShirtColor(actor.shirtColor);
			itsPreviewController.setPantsColor(actor.pantsColor);
			itsPreviewController.geometryChanged(actor.width, actor.height, actor.expression);
		}
	}
	
	public void editActor(int index)
	{
		
		Actor actor = itsActorsModel.itsActorTableModel.getActor(index);
		Actor editedActor = new Actor(actor);
		IActorChangeExecutor executor = new ActorModifiedExecutor(this);
		ActorEditorController actorEditorController= new ActorEditorController(this, editedActor, executor);
		JDialog dialog = actorEditorController.createView(itsMainFrame);
		
		dialog.pack();
		dialog.setVisible(true);
		itsPreviewController.setShirtColor(actor.shirtColor);
		itsPreviewController.setPantsColor(actor.pantsColor);
		itsPreviewController.geometryChanged(actor.width, actor.height, actor.expression);
	}
	
	public void addActor()
	{
		Actor newActor = new Actor();
		IActorChangeExecutor executor = new NewActorExecutor(this);
		ActorEditorController actorEditorController= new ActorEditorController(this, newActor, executor);
		JDialog dialog = actorEditorController.createView(itsMainFrame);
		
		dialog.pack();
		dialog.setVisible(true);
	}
	
	public void actorModified(Actor fromActor)
	{
		int index = itsActorsView.itsActorsTable.getSelectedRow();
		if(index > -1)
		{
			Actor actorToModify = itsActorsModel.itsActorTableModel.actorsVector.get(index);
			actorToModify.copyFrom(fromActor);
			itsActorsModel.itsActorTableModel.fireTableRowsUpdated(index, index);
		}
	}
	
	public void actorAdded(Actor newActor)
	{
		int index = itsActorsModel.itsActorTableModel.actorsVector.size();
		itsActorsModel.itsActorTableModel.actorsVector.add(newActor);
		itsActorsModel.itsActorTableModel.fireTableRowsInserted(index, index);
		itsActorsView.itsActorsTable.setRowSelectionInterval(index, index);
		itsPreviewController.setVisible(true);
		itsActorsView.edtActorBtn.setEnabled(true);
		itsActorsView.rmvActorBtn.setEnabled(true);
	}
	
	public void tableEmpty()
	{
		itsPreviewController.setVisible(false);
	}
	
	private ActorsView itsActorsView;
	private ActorsModel itsActorsModel;
	private PreviewController itsPreviewController;
	private JFrame itsMainFrame;
}

/*
 * ActorModifiedExecutor
 * A callback executor, called after the user edited an actor
 */
class ActorModifiedExecutor implements IActorChangeExecutor
{

	public ActorModifiedExecutor(ActorsController controller)
	{
		itsActorsController = controller;
	}
	@Override
	public void ActorChanged(Actor actor)
	{
		itsActorsController.actorModified(actor);
	}
	private ActorsController itsActorsController;
}

/*
 * NewActorExecutor
 * A callback executor, invoked when a new actor is created
 */
class NewActorExecutor implements IActorChangeExecutor
{

	public NewActorExecutor(ActorsController controller)
	{
		itsActorsController = controller;
	}
	@Override
	public void ActorChanged(Actor actor)
	{
		itsActorsController.actorAdded(actor);
	}
	private ActorsController itsActorsController;
}